import 'package:flutter/material.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/fan.dart';
import 'package:homepad/logger/logger.dart';
import 'package:homepad/iot/air_conditioner.dart';
import 'package:homepad/models/nova_model.dart';
import 'package:homepad/styles.dart';
import 'package:homepad/views/air_conditioner_mode_view.dart';
import 'package:homepad/views/light_brightness_view.dart';
import 'package:homepad/views/panel_header.dart';
import 'package:homepad/views/panel_title_header.dart';
import 'package:provider/provider.dart';

class PanelAirConditioner extends StatelessWidget {
  final GestureTapCallback? onCancel;
  final AirConditioner air;

  const PanelAirConditioner({super.key, required this.air, required this.onCancel});

  @override
  Widget build(BuildContext context) {
    final MyStyles styles = Theme.of(context).extension<MyStyles>()!;
    var systemMode = context.select<NovaModel, AirConditionerSystemMode>((nova) => air.systemMode);
    var targetTemperature = context.select<NovaModel, double>((_) => air.targetTemperature);

    var fan = context.select<NovaModel, Fan>((nova) => nova.config.getChildNode(air.endpointId, DeviceType.fan) as Fan);
    var fanSpeed = context.select<NovaModel, int>((_) => fan.fanSpeed);

    var setTemperatureEnable = context.select<NovaModel, bool>((_) {
      switch (air.systemMode) {
        case AirConditionerSystemMode.auto ||
              AirConditionerSystemMode.cool ||
              AirConditionerSystemMode.heat ||
              AirConditionerSystemMode.dry:
          return true;
        default:
          return false;
      }
    });

    var setFanSpeedEnable = context.select<NovaModel, bool>((_) {
      switch (air.systemMode) {
        case AirConditionerSystemMode.auto ||
              AirConditionerSystemMode.cool ||
              AirConditionerSystemMode.heat ||
              AirConditionerSystemMode.fanOnly:
          return true;
        default:
          return false;
      }
    });

    final backgroundDecoration = BoxDecoration(borderRadius: styles.radius, color: styles.panelBackgroundColor);
    final foregroundDecoration = backgroundDecoration.copyWith(color: styles.panelDisableForegroundColor);

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
            width: styles.panelWidgetWidth,
            height: styles.panelWidgetHeight,
            decoration: backgroundDecoration,
            child: Column(
              children: [
                PanelServiceHeader(node: air),
                const Spacer(),
                AirConditionerModeView(
                    current: systemMode,
                    onChanged: (value) {
                      logger.info("target_mode: $value");
                      air.setSystemMode(value);
                    }),
                const Spacer(),
              ],
            )),
        SizedBox(width: styles.panelWidgetSpace),
        Container(
            width: styles.panelWidgetWidth,
            height: styles.panelWidgetHeight,
            decoration: backgroundDecoration,
            foregroundDecoration: setTemperatureEnable ? null : foregroundDecoration,
            child: GestureDetector(
                child: Column(
              children: [
                PanelTitleHeader(
                  title: "温度",
                  subtitle: "${targetTemperature.toInt()}",
                  extra: ".${(targetTemperature * 10).toInt() % 10}°",
                  width: styles.panelWidgetWidth,
                ),
                const Spacer(),
                LightBrightnessView(
                  min: 16,
                  max: 30,
                  step: 0.5,
                  current: targetTemperature,
                  enable: setTemperatureEnable,
                  onChanged: (value) {
                    air.setTargetTemperature(value);
                  },
                ),
                const Spacer(),
              ],
            ))),
        SizedBox(width: styles.panelWidgetSpace),
        Container(
            width: styles.panelWidgetWidth,
            height: styles.panelWidgetHeight,
            decoration: backgroundDecoration,
            foregroundDecoration: setFanSpeedEnable ? null : foregroundDecoration,
            child: Column(
              children: [
                PanelTitleHeader(
                    title: "风速",
                    subtitle: "$fanSpeed",
                    extra: "%",
                    width: styles.panelWidgetWidth),
                const Spacer(),
                LightBrightnessView(
                  min: 1,
                  max: 100,
                  step: 1,
                  current: fanSpeed.toDouble(),
                  enable: setFanSpeedEnable,
                  onChanged: (value) {
                    fan.setFanSpeed(value.toInt());
                  },
                ),
                const Spacer(),
              ],
            )),
      ],
    );
  }
}
